import { localCache } from '@/utils/cache'
import MyAxios from './request'

import { userConfig } from './confing/index'

const MyRequest = new MyAxios({
  baseURL: userConfig.baseURL,
  timeout: userConfig.timeout,

  //拦截器
  interceptors: {
    requestSuccessFn(config) {
      // 请求拦截器
      const token = localCache.getCache('token')
      if (token && config.headers) {
        config.headers.Authorization = `${token}` // 请求时自动携带token
      }
      return config
    },
    requestFailureFn(err) {
      return err
    },
    responseSuccessFn(res) {
      // 相应拦截器
      return res
    },
    resPonseFailureFn(err) {
      return err
    }
  }
})

export { MyRequest }
